package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.Admin;
import dao.AdminDAO;

/**
 * Servlet implementation class AddUserServlet
 */
@WebServlet("/AddAdminServlet")
public class AddAdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddAdminServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub	

		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("UTF-8");
		//response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			String user_name = request.getParameter("user_name");
			String password = request.getParameter("user_password");
			String user_tel = request.getParameter("user_tel");
			Admin admin = new Admin(user_name,password,user_tel);
			AdminDAO.getInstance().addAdmin(admin);
			request.setAttribute("result_information","添加成功！" );
			request.setAttribute("return_page","ManageAdminServlet" );
			request.setAttribute("return_page_name","管理员账号列表" );
			RequestDispatcher view = request.getRequestDispatcher("result_information.jsp");
			view.forward(request, response);

		}catch(Exception e){
			out.print("添加失败");
			e.printStackTrace();
		}
	}

}
